package it.polimi.entities.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import it.polimi.entitiesDTO.PersonalizedPacketDTO;
import it.polimi.entitiesManagers.HotelMgr;
import it.polimi.entitiesManagers.PersonalizedPacketMgr;
import it.polimi.entitiesManagers.TransportMgr;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;


@ManagedBean(name = "PersonalizationBean")
@SessionScoped
public class PersonalizationBean {
	
	@EJB
	PersonalizedPacketMgr pmgr;
	@EJB
	private HotelMgr hmgr;
	
	
	private int id;	
	private int id2;
	private PersonalizedPacketDTO personalizedPacketDTO;		
	private List<String> hotels;	
	private String city;	
	private int hotelID;
	private List<Integer> myPackets;
	private List<String> cityAttractions;
	private List<String> packetAttractions;
	private int addAttractionID;
	private int removeAttractionID;
	private List<String> departureFlights;
	private List<String> returnFlights;
	private int departureFlight;
	private int returnFlight;
	private Date departure;
	private Date return_;
	private int partecipants;
	
	
	public PersonalizationBean() {
		clearAll();
	}
	
	private void clearAll(){
		this.setId(0);
		this.personalizedPacketDTO = new PersonalizedPacketDTO();
		this.hotels = new ArrayList<String>();
		this.myPackets = new ArrayList<Integer>();
		this.hotelID=0;
		this.city=new String();
		this.cityAttractions = new ArrayList<String>();
		this.packetAttractions = new ArrayList<String>();
		this.addAttractionID=0;
		this.removeAttractionID=0;
		this.departureFlights = new ArrayList<String>();
		this.returnFlights = new ArrayList<String>();
		this.departureFlight = 0;
		this.returnFlight = 0;
		this.departure = new Date();
		this.return_ = new Date();
		this.partecipants=0;
		this.id2=0;
	}
	
	public String register() {
		pmgr.save(this.personalizedPacketDTO, id);
		id=0;
		return "/user/index";
	}
	
	public String registerPersonalized() {
		boolean check = false;
		check = pmgr.getPacketUser(id2);
		if (check){
		pmgr.save(id2);
		clearAll();
		return "/user/index";
		}
		else return "/user/errorPage";
		
	}
	
	public String redirectToModification(){
		boolean check = false;
		check = pmgr.getPacketUser(id);
		if(!check){
		showAttractions();
		showHotels();
		showFlights();
		return "/user/packetPersonalization";
		}
		else return "/user/errorPage";
	}
	
	public void setDates(){
		System.out.println(id +"dates");
		pmgr.setPacketDates(id, departure, return_);
	}
	
	public String confirm(){
		if(partecipants<1){
			return "/user/errorPage";
		}
		else
		pmgr.confirmPacket(id,partecipants);
		clearAll();
		return "/user/index";
	}
	
	public void addAttraction(){
		System.out.println("aaaa" + addAttractionID);
		pmgr.addAttraction(id, addAttractionID);
		showAttractions();
		addAttractionID=0;
	}
	
	public void removeAttraction(){
		pmgr.removeAttraction(id, removeAttractionID);
		showAttractions();
		removeAttractionID=0;
	}
	public void showAttractions(){
		this.packetAttractions=pmgr.getPacketAttractions(id);
		this.cityAttractions=pmgr.getCityAttractions(id);
		
	}
	public void showHotels(){
		hotels = pmgr.getAlternativeHotels(id);
	}
	
	public void showFlights(){
		departureFlights = pmgr.getDepartureFlights(id);
		returnFlights = pmgr.getReturnFlights(id);
	}
	
	public void saveHotel(){
		pmgr.changeHotel(id, hotelID);
		//clearAll();
	}
	
	public void changeFlights(){
		pmgr.changeFlights(id, departureFlight, returnFlight);
	}
	
	public String showPackets(){
		myPackets = pmgr.fillMyPackets();
		return "/user/myPackets";
	}
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public PersonalizedPacketDTO getPersonalizedPacketDTO() {
		return personalizedPacketDTO;
	}

	public void setPersonalizedPacketDTO(PersonalizedPacketDTO personalizedPacketDTO) {
		this.personalizedPacketDTO = personalizedPacketDTO;
	}

	public List<String> getHotels() {
		return hotels;
	}

	public void setHotels(List<String> hotels) {
		this.hotels = hotels;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public int getHotelID() {
		return hotelID;
	}

	public void setHotelID(int hotelID) {
		this.hotelID = hotelID;
	}

	public List<Integer> getMyPackets() {
		return myPackets;
	}

	public void setMyPackets(List<Integer> myPackets) {
		this.myPackets = myPackets;
	}

	public List<String> getCityAttractions() {
		return cityAttractions;
	}

	public void setCityAttractions(List<String> cityAttractions) {
		this.cityAttractions = cityAttractions;
	}

	public List<String> getPacketAttractions() {
		return packetAttractions;
	}

	public void setPacketAttractions(List<String> packetAttractions) {
		this.packetAttractions = packetAttractions;
	}

	

	public int getAddAttractionID() {
		return addAttractionID;
	}

	public void setAddAttractionID(int addAttractionID) {
		this.addAttractionID = addAttractionID;
	}

	public int getRemoveAttractionID() {
		return removeAttractionID;
	}

	public void setRemoveAttractionID(int removeAttractionID) {
		this.removeAttractionID = removeAttractionID;
	}

	public List<String> getDepartureFlights() {
		return departureFlights;
	}

	public void setDepartureFlights(List<String> departureFlights) {
		this.departureFlights = departureFlights;
	}

	public List<String> getReturnFlights() {
		return returnFlights;
	}

	public void setReturnFlights(List<String> returnFlights) {
		this.returnFlights = returnFlights;
	}

	public int getDepartureFlight() {
		return departureFlight;
	}

	public void setDepartureFlight(int departureFlight) {
		this.departureFlight = departureFlight;
	}

	public int getReturnFlight() {
		return returnFlight;
	}

	public void setReturnFlight(int returnFlight) {
		this.returnFlight = returnFlight;
	}

	public Date getDeparture() {
		return departure;
	}

	public void setDeparture(Date departure) {
		this.departure = departure;
	}

	public Date getReturn_() {
		return return_;
	}

	public void setReturn_(Date return_) {
		this.return_ = return_;
	}

	public int getPartecipants() {
		return partecipants;
	}

	public void setPartecipants(int partecipants) {
		this.partecipants = partecipants;
	}

	public int getId2() {
		return id2;
	}

	public void setId2(int id2) {
		this.id2 = id2;
	}

}
